
const User=require('../model/user.model')
class UserService{
    async createUser(username,password,nickname){
        //写入数据
       const res= await User.create({username:username,password:password,nickname:nickname})

        return res.dataValues
    }
    async CheckInfo({id,username,password}){
        const whereOpt={}
        id && Object.assign(whereOpt,{id})
        username && Object.assign(whereOpt,{username})
        password && Object.assign(whereOpt,{password})
    const res = await User.findOne({
        attributes:['id','username','password','nickname','userImage'],
        where:whereOpt
    })
    return res? res.dataValues:null

    }
    async updataById({id,password,nickname,userImage}){
        const whereOpt={id}
        const newUser={}
        userImage && Object.assign(newUser,{userImage})
        nickname && Object.assign(newUser,{nickname})
        password && Object.assign(newUser,{password})
        const res =await User.update(newUser,{where:whereOpt})
        return [false,true][res[0]]
    }
    async CheckData(sheId){
        const res= await User.findOne({
            attributes:['id','username','nickname','userImage'],
            where:{

               id:sheId
            }
        })
        return res
    }

}

module.exports=new UserService()